/**
 * 缓存token
 * @param {string} token
 */
export const setToken = token => {
  sessionStorage.setItem('token', token)
}

/**
 * 获取token缓存
 * @returns {string|null}
 */
export const getToken = () => {
  const token = sessionStorage.getItem('token')
  if (token) {
    return token
  }
  return null
}

/**
 * 缓存用户信息
 * @param {{}} info
 */
export const setUserInfo = info => {
  sessionStorage.setItem('user_info', JSON.stringify(info))
}

/**
 * 获取用户信息缓存
 * @returns {{}|null}
 */
export const getUserInfo = () => {
  const info = sessionStorage.getItem('user_info')
  if (info) {
    return info
  }
  return null
}

/**
 * 锚点滚动
 * @param { string | number } selector
 */
export const goAnchor = (selector) => {
  // 移动距离
  let top = 0
  // 当前滚动条位置
  const scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop
  // 若为指定距离
  if (typeof selector === 'number') {
    top = selector - scrollTop
  } else {
    const anchor = document.querySelector(selector) || { offsetTop: 0 }
    top = anchor.offsetTop - scrollTop
  }
  window.scrollBy({ top, behavior: 'smooth' })
}
